import elib.Osoba;
import elib.Klient;
import elib.Kierownik;
import elib.Bibliotekarz;
import elib.Logowanie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/LogInServlet")
public class LogInServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    public LogInServlet() {
        super();
        // TODO Auto-generated method stub
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		String login = request.getParameter("login");
		String haslo = request.getParameter("haslo");
		
		Logowanie log = new Logowanie(login, haslo);
		Osoba osoba = log.login();
		if( osoba != null ) {
			if(osoba instanceof Klient){
				session.setAttribute("osoba", osoba);
				response.sendRedirect("user.jsp");
			} else if(osoba instanceof Bibliotekarz) {
				session.setAttribute("osoba", osoba);
				response.sendRedirect("librarian.jsp");
			} else if(osoba instanceof Kierownik) {
				session.setAttribute("osoba", osoba);
				response.sendRedirect("admin.jsp");
			}
		} else {
			session.setAttribute("err", "error");
			response.sendRedirect("index.jsp");
		}
		out.close();
	}
}